#include <taskDemo51.h>

// #####################task hello begin############################
#define taskIntervalMs_hello 1000
static DEV_C51_XDATA_FLAG sysTmTp taskLastUpMs_hello = 0;
#if TASK_USE_INIT_QUIT_FLAG
static void taskFuncInit_hello()
{
    logdebugc("");
}
static void taskFuncQuit_hello()
{
    logdebugc("");
}
#endif
static void taskFuncUp_hello()
{
    logdebugc("");
}
// #####################task hello end############################

// #####################task check1 begin############################
#define taskIntervalMs_check1 10000
static DEV_C51_XDATA_FLAG sysTmTp taskLastUpMs_check1 = 0;
#if TASK_USE_INIT_QUIT_FLAG
static void taskFuncInit_check1()
{
    logdebugc("");
}
static void taskFuncQuit_check1()
{
    logdebugc("");
}
#endif
static void taskFuncUp_check1()
{
    logdebugc("");
}
// #####################task check1 end############################

typedef enum
{
    taskDoId_hello = 0,
    taskDoId_check1,
    taskDoId_Max,
} taskDoIdEnum;
#if TASK_USE_INIT_QUIT_FLAG
void taskMgr51DemoInit()
{
#define taskDoOneInit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncInitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneInit(hello);
            taskDoOneInit(check1);
        default:
        {
            break;
        }
        }
    }
}

void taskMgr51DemoQuit()
{
#define taskDoOneQuit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncQuitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneQuit(hello);
            taskDoOneQuit(check1);
        default:
        {
            break;
        }
        }
    }
}
#endif
void taskMgr51DemoUp()
{
    static uint8_t tmpGlobalUpId = 0;
    uint8_t i;
    DEV_C51_XDATA_FLAG sysTmTp tmpTmMs;
    tmpTmMs = sysTmNow();
#define taskDoOneUp(tname)                                            \
    case taskEnumGet(tname):                                          \
    {                                                                 \
        if (tmpTmMs - taskLastUpGet(tname) >= taskIntervalGet(tname)) \
        {                                                             \
            taskFuncUpGet(tname)();                                   \
            taskLastUpGet(tname) = sysTmNow();                        \
        }                                                             \
        break;                                                        \
    }
    for (i = 0; i < taskDoId_Max; i++)
    {
        tmpGlobalUpId = CYCLE_ADD(tmpGlobalUpId, i, taskDoId_Max);
        switch (tmpGlobalUpId)
        {
            taskDoOneUp(hello);
            taskDoOneUp(check1);
        default:
        {
            break;
        }
        }
        if (sysTmNow() - tmpTmMs >= TASK_RUN_MAX_MS)
        {
            break;
        }
    }
}
